using System;
using System.Collections.Generic;
using System.Data;
using System.Resources;
using PluginFrame;

namespace PcrExperimentProject
{
	[Serializable]
	public class AmplificationSegment : IAmplificationSegment, ISegment
	{
		[NonSerialized]
		protected ResourceManager mResourceManager;

		[NonSerialized]
		protected IApplication mApplication;

		protected IPcrProgram mPcrProgram;

		protected string mName;

		protected int mIndex;

		protected bool mIsAvailable;

		private DataTable mStepTable;

		private int mFluoscenceStep;

		protected int mCycle;

		protected List<object> mReserved;

		protected double mLastTemperature;

		public bool IsAvailable
		{
			get
			{
				return mIsAvailable;
			}
			set
			{
				mIsAvailable = value;
			}
		}

		public int Index
		{
			get
			{
				return mIndex;
			}
			set
			{
				mIndex = value;
			}
		}

		public int FluoscenceStep
		{
			get
			{
				return mFluoscenceStep;
			}
			set
			{
				mFluoscenceStep = value;
			}
		}

		public string Name
		{
			get
			{
				return mName;
			}
			set
			{
				mName = value;
			}
		}

		public DataTable StepTable
		{
			get
			{
				return mStepTable;
			}
		}

		public int Cycle
		{
			get
			{
				return mCycle;
			}
			set
			{
				mCycle = value;
			}
		}

		protected AmplificationSegment()
		{
		}

		private bool CompareProgramDoubleVale(object thisRow, object compareRow)
		{
			try
			{
				double result;
				bool flag = double.TryParse(thisRow.ToString(), out result);
				double result2;
				bool flag2 = double.TryParse(compareRow.ToString(), out result2);
				if (flag && flag2)
				{
					return result == result2;
				}
				if (!flag && !flag2)
				{
					return true;
				}
				return false;
			}
			catch (Exception)
			{
				return false;
			}
		}

		private bool CompareProgramBoolVale(object thisRow, object compareRow)
		{
			try
			{
				bool result;
				bool flag = bool.TryParse(thisRow.ToString(), out result);
				bool result2;
				bool flag2 = bool.TryParse(compareRow.ToString(), out result2);
				if (flag && flag2)
				{
					return result == result2;
				}
				if (!flag && !flag2)
				{
					return true;
				}
				return false;
			}
			catch (Exception)
			{
				return false;
			}
		}

		public bool IsEqual(ISegment segment)
		{
			IAmplificationSegment amplificationSegment = segment as IAmplificationSegment;
			if (amplificationSegment == null)
			{
				return false;
			}
			if (mIsAvailable != segment.IsAvailable)
			{
				return false;
			}
			if (!mIsAvailable)
			{
				return true;
			}
			if (mFluoscenceStep != amplificationSegment.FluoscenceStep)
			{
				return false;
			}
			if (mCycle != amplificationSegment.Cycle)
			{
				return false;
			}
			if (mStepTable.Rows.Count != amplificationSegment.StepTable.Rows.Count)
			{
				return false;
			}
			for (int i = 0; i < mStepTable.Rows.Count; i++)
			{
				DataRow dataRow = mStepTable.Rows[i];
				DataRow dataRow2 = amplificationSegment.StepTable.Rows[i];
				if (!CompareProgramDoubleVale(dataRow["Temperature"], dataRow2["Temperature"]))
				{
					return false;
				}
				if (dataRow["HoldTime"].ToString() != dataRow2["HoldTime"].ToString())
				{
					return false;
				}
				if (!CompareProgramBoolVale(dataRow["Fluorescence"], dataRow2["Fluorescence"]))
				{
					return false;
				}
				if (!CompareProgramBoolVale(dataRow["Advanced"], dataRow2["Advanced"]))
				{
					return false;
				}
				if (Convert.ToBoolean(dataRow["Advanced"]))
				{
					if (!CompareProgramBoolVale(dataRow["TemperatureVariation"], dataRow2["TemperatureVariation"]))
					{
						return false;
					}
					if (!CompareProgramDoubleVale(dataRow["TemperatureValue"], dataRow2["TemperatureValue"]))
					{
						return false;
					}
					if (!CompareProgramBoolVale(dataRow["TimeVariation"], dataRow2["TimeVariation"]))
					{
						return false;
					}
					if (dataRow["TimeValue"].ToString() != dataRow2["TimeValue"].ToString())
					{
						return false;
					}
					if (!CompareProgramDoubleVale(dataRow["Rising/FallingRate"], dataRow2["Rising/FallingRate"]))
					{
						return false;
					}
				}
			}
			return true;
		}

		public AmplificationSegment(string segmentName, int index, IPcrProgram pcrProgram)
		{
			mIsAvailable = false;
			mPcrProgram = pcrProgram;
			mName = segmentName;
			mIndex = index;
			mStepTable = new DataTable();
			mStepTable.Columns.Add("StepName", typeof(string));
			mStepTable.Columns.Add("Temperature", typeof(double));
			mStepTable.Columns.Add("HoldTime", typeof(string));
			mStepTable.Columns.Add("Fluorescence", typeof(bool));
			mStepTable.Columns.Add("Advanced", typeof(bool));
			mStepTable.Columns.Add("TemperatureVariation", typeof(bool));
			mStepTable.Columns.Add("TemperatureValue", typeof(double));
			mStepTable.Columns.Add("TimeVariation", typeof(bool));
			mStepTable.Columns.Add("TimeValue", typeof(string));
			mStepTable.Columns.Add("Rising/FallingRate", typeof(double));
			CreateNewStep();
			mCycle = 1;
			mFluoscenceStep = 0;
		}

		public DataRow CreateNewStep()
		{
			if (mApplication == null && mPcrProgram != null)
			{
				mApplication = mPcrProgram.GetApplication();
				mResourceManager = ResourceManager.CreateFileBasedResourceManager("PcrExperimentProject", mApplication.GetResourceFolder() + GetType().Namespace + "\\", null);
			}
			string text = "Step";
			try
			{
				if (mResourceManager != null)
				{
					text = mResourceManager.GetString("StepString");
				}
			}
			catch (Exception)
			{
			}
			DataRow dataRow = mStepTable.NewRow();
			dataRow["StepName"] = text + " " + (mStepTable.Rows.Count + 1);
			dataRow["Temperature"] = 95.0;
			dataRow["HoldTime"] = "00:20";
			dataRow["Fluorescence"] = false;
			dataRow["Advanced"] = false;
			dataRow["TemperatureVariation"] = false;
			dataRow["TemperatureValue"] = 0;
			dataRow["TimeVariation"] = false;
			dataRow["TimeValue"] = 0;
			dataRow["Rising/FallingRate"] = 0;
			mStepTable.Rows.Add(dataRow);
			mStepTable.AcceptChanges();
			return dataRow;
		}

		public object GetReservedValue(int index)
		{
			return mReserved[index];
		}

		public void SetReservedValue(int index, object obj)
		{
			mReserved[index] = obj;
		}
	}
}
